home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Stuff / 3D_Reality / Shaders / Shader_Source / SD_BlueMarble.sl < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.9 KB  |  97 lines

  1. /*************************************************************************
  2. *    Copyright (c) 1989-1992 Stone Design Corp.  All rights reserved. 
  3. *    programmer:    Bill Bumgarner
  4. *    File name:    SD_BlueMarble.sl
  5. *    Date:        Jul 25 1992 
  6. *    Purpose:    This is the blue_marble shader from The RenderMan
  7.             Companion (p.355).  It has been modified such that
  8.             the colors used to define the spline across which the
  9.             colors for the marble texture are interpolated.
  10.     Variables:
  11.             Ka:        percentage of ambient light used
  12.             Kd:        percentage of diffuse light used
  13.             Ks:        factor of effect for the specular highlight
  14.             roughness:    roughness factor for surface; used w/highlight
  15.             specularcolor:  color used for specular highlight
  16.             
  17.             color1..13:    colors along the spline used for interpolation
  18.                     of the marble pattern.
  19. ***************************************************************************/
  20. surface
  21. SD_BlueMarble(
  22.           float   Ks    = .4, 
  23.                   Kd    = .6, 
  24.                   Ka    = .6,
  25.                   roughness = .1,
  26.                   txtscale = 1;
  27.           color   specularcolor = 1;
  28.       
  29.       color      color1 = color (0.25, 0.25, 0.35),      /* pale blue      */
  30.         color2 = color (0.25, 0.25, 0.35),  /* pale blue        */
  31.         color3 = color (0.20, 0.20, 0.30),  /* medium blue      */
  32.         color4 = color (0.20, 0.20, 0.30),  /* medium blue      */
  33.         color5 = color (0.20, 0.20, 0.30),  /* medium blue      */
  34.         color6 = color (0.25, 0.25, 0.35),  /* pale blue        */
  35.         color7 = color (0.25, 0.25, 0.35),  /* pale blue        */
  36.         color8 = color (0.15, 0.15, 0.26),  /* medium dark blue */
  37.         color9 = color (0.15, 0.15, 0.26),  /* medium dark blue */
  38.         color10 = color (0.10, 0.10, 0.20),  /* dark blue        */
  39.         color11 = color (0.10, 0.10, 0.20),  /* dark blue        */
  40.         color12 = color (0.25, 0.25, 0.35),  /* pale blue        */
  41.         color13 = color (0.10, 0.10, 0.20);   /* dark blue        */
  42. )
  43. {
  44.     point PP;            /* scaled point in shader space */
  45.     float csp;           /* color spline parameter */
  46.     point Nf;            /* forward-facing normal */
  47.     point V;             /* for specular() */
  48.     float pixelsize, twice, scale, weight, turbulence;
  49.  
  50.     /* Obtain a forward-facing normal for lighting calculations. */
  51.     Nf = faceforward( normalize(N), I);
  52.     V = normalize(-I);
  53.  
  54.     /*
  55.      * Compute "turbulence" a la [PERLIN85]. Turbulence is a sum of 
  56.      * "noise" components with a "fractal" 1/f power spectrum. It gives the
  57.      * visual impression of turbulent fluid flow (for example, as in the 
  58.      * formation of blue_marble from molten color splines!). Use the 
  59.      * surface element area in texture space to control the number of 
  60.      * noise components so that the frequency content is appropriate 
  61.      * to the scale. This prevents aliasing of the texture.
  62.      */
  63.     PP = transform("shader", P) * txtscale;
  64.     pixelsize = sqrt(area(PP));
  65.     twice = 2 * pixelsize;
  66.     turbulence = 0;
  67.     for (scale = 1; scale > twice; scale /= 2) 
  68.         turbulence += scale * noise(PP/scale);
  69.  
  70.     /* Gradual fade out of highest-frequency component near limit */
  71.     if (scale > pixelsize) {
  72.         weight = (scale / pixelsize) - 1;
  73.         weight = clamp(weight, 0, 1);
  74.         turbulence += weight * scale * noise(PP/scale);
  75.     }
  76.  
  77.     /*
  78.      * Magnify the upper part of the turbulence range 0.75:1
  79.      * to fill the range 0:1 and use it as the parameter of
  80.      * a color spline through various shades of blue.
  81.      */
  82.     csp = clamp(4 * turbulence - 3, 0, 1);
  83.     Ci = color spline(csp, color1, color2, color3, color4, color5, color6,
  84.                 color7, color8, color9, color10, color11, 
  85.                 color12, color13);
  86.  
  87.     /* Multiply this color by the diffusely reflected light. */
  88.     Ci *= Ka*ambient() + Kd*diffuse(Nf);
  89.  
  90.     /* Adjust for opacity. */
  91.     Oi = Os;
  92.     Ci = Ci * Oi;
  93.  
  94.     /* Add in specular highlights. */
  95.     Ci += specularcolor * Ks * specular(Nf,V,roughness);
  96. }
  97.